home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5001 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: pointers to pointers that point to structs?
  5. Date: 11 Feb 1996 17:45:33 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4flrid$85i@umbc10.umbc.edu>
  8. References: <311C1B4E.217F@mars.superlink.net>
  9. NNTP-Posting-Host: umbc10.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Michael Rizzo  <rizzom@mars.superlink.net> wrote:
  13. |>   I am having trouble dereferencing a pointer to a pointer to a struct.
  14. |> For simplicity just say the struct is:
  15. |> struct test
  16. |>   {
  17. |>   char teststr[10];
  18. |>   int  testint;
  19. |>   }
  20.  
  21. You may want a ; after the struct definition.
  22.  
  23. |> Now I have a function that just wants to print elements of the 
  24. |> structure:
  25. |> 
  26. |> void f(test **temp)
  27.  
  28. You mean:
  29.  
  30. void f (struct test **temp)
  31.  
  32. |> {
  33. |> printf("%s  %d",(please fill in the blank))
  34.  
  35. Well assuming dereferencing the pointers at both levels are legal then
  36. just do:
  37.  
  38. printf ("%s  %d", (**temp).teststr, (**temp).testint);
  39.  
  40. Also don't forget to #include <stdio.h> for the printf() calls...
  41.  
  42. |> }
  43. |> 
  44. |> How to I get to test.teststr and test.testint from within the funtion.
  45. |> I have tried temp->teststr.  I'm still pretty new to programming in C, 
  46. |> and the book I'm using does not go into such topics, so any help would 
  47. |> be greatly appreciated.  
  48.  
  49. Well temp->teststr would be correct if you only had one level of pointers.
  50. So an alternate way would be:
  51.  
  52. printf ("%s  %d", (*temp)->teststr, (*temp)->testint);
  53.  
  54. This is not necessarily the only 2 ways although both will do what you
  55. have asked.
  56. -- 
  57. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  58.  
  59. Jonas J. Schlein  (schlein@gl.umbc.edu)
  60.